import request from "../http/index";
import {getTonke} from "@/utils/user.js";

import {
  Message
} from 'element-ui';
export default function ({
  app,
  $axios,
  store,
}, inject) {
  $axios.onRequest((config) => {
    // 相关配置
    config.headers["Authorization"] = getTonke();
  });
  $axios.onResponse((response) => {
    let code = response.data.code;
    let msg = response.data.msg;
  
    if (code !== 200) {
      if(code == 401){
        Message({
          message:"登录过期，请重新登录",
          type: 'error',
          duration: 5 * 1000
        })
        localStorage.removeItem("token_zb");
        localStorage.removeItem("userInfo");
        app.router.push("/");
        window.location.reload();
        return
      };
      Message({
        message: msg,
        type: 'error',
        duration: 5 * 1000
      })
      return;
    }
    return response.data;
  });
  $axios.onError(error => {
    Message({
      message:"服务器异常",
      type: 'error',
      duration: 5 * 1000
    })
  })
  request($axios, (api) => {
    inject("api", api)
  })
}
